package cn.iocoder.yudao.module.hs.controller.admin.scan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.materialbox.vo.MaterialBoxRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.scan.vo.ScanInfoRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.scan.vo.ScanManualAdjustReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.scan.vo.ScanMergeReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.scan.vo.ScanPushReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.module.hs.service.scan.ScanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 扫码相关")
@RestController
@RequestMapping("/hs/scan")
@Validated
public class ScanController {

    @Resource
    private ScanService scanService;

    @PostMapping("/push")
    @Operation(summary = "将扫码出来的信息保存到缓存中，作为已选备选")
    public CommonResult<List<MaterialDO>> push(@Valid @RequestBody ScanPushReqVO scanPushReqVO) {
        return success(scanService.pushScanMaterials(scanPushReqVO));
    }

    @GetMapping("/get-picked-list")
    @Operation(summary = "获取已拾取清单")
    public CommonResult<List<ScanInfoRespVO>> getToBePickedMaterialList(@RequestParam("pickTaskId") Long pickTaskId) {
        return success(scanService.getPickedList(pickTaskId));
    }

    @PostMapping("/merge")
    @Operation(summary = "合托")
    public CommonResult<Boolean> push(@Valid @RequestBody ScanMergeReqVO scanMergeReqVO) {
        scanService.merge(scanMergeReqVO);
        return success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建托盘")
    public CommonResult<Boolean> create(@RequestParam("code") String code) {
        scanService.create(code);
        return success(true);
    }

    @GetMapping("/get-tobepicked-material-list")
    @Operation(summary = "通过RFID编码获取待拣取的物料明细列表")
    public CommonResult<List<MaterialRespVO>> getToBePickedMaterialList(@RequestParam("pickTaskId") Long pickTaskId,
                                                                        @RequestParam("rfidCode") String rfidCode,
                                                                        @RequestParam("light") Integer light) {
        return success(scanService.getToBePickedMaterialList(pickTaskId, rfidCode, light));
    }

    @GetMapping("/get-tobepicked-materialbox-list")
    @Operation(summary = "通过RFID编码获取待拣取的物料箱列表")
    public CommonResult<List<MaterialBoxRespVO>> getToBePickedMaterialBoxList(@RequestParam("pickTaskId") Long pickTaskId,
                                                                              @RequestParam("rfidCode") String rfidCode,
                                                                              @RequestParam("light") Integer light) {
        return success(scanService.getToBePickedMaterialBoxList(pickTaskId, rfidCode, light));
    }

    @GetMapping("/light-flash")
    @Operation(summary = "给指定标签发送闪灯指令")
    public CommonResult<Boolean> lightFlash(@RequestParam("rfidCode") String rfidCode, @RequestParam("light") Integer light) {
        scanService.lightFlash(rfidCode, light);
        return success(true);
    }

    @GetMapping("/tray-or-rfid")
    @Operation(summary = "通过扫码获取托盘、RFID的统计信息")
    public CommonResult<ScanInfoRespVO> getTrayRfidInfo(@RequestParam("code") String code) {
        return success(scanService.getTrayRfidInfo(code));
    }

    @GetMapping("/material-or-box")
    @Operation(summary = "获取托盘上的物料、物料箱列表信息")
    public CommonResult<List<ScanInfoRespVO>> getMaterialBoxInfo(@RequestParam(value = "pickTaskId", required = false) Long pickTaskId,
                                                                 @RequestParam(value = "shipOrderId", required = false) Long shipOrderId,
                                                                 @RequestParam("code") String code) {
        return success(scanService.getMaterialBoxInfo(pickTaskId, shipOrderId, code));
    }

    @GetMapping("/material-box-code")
    @Operation(summary = "获取物料、物料箱信息")
    public CommonResult<ScanInfoRespVO> getMaterialBoxByCode(@RequestParam("code") String code) {
        return success(scanService.getMaterialBoxByCode(code));
    }

    @PostMapping("/manual-adjust")
    @Operation(summary = "手动调整")
    public CommonResult<Boolean> manualAdjust(@Valid @RequestBody ScanManualAdjustReqVO reqVO) {
        scanService.manualAdjust(reqVO);
        return success(true);
    }
}
